package com.tgy.algorithm.base;

/**
 * 快慢指针
 * 1)输入链表头节点，奇数长度返回中点，偶数长度返回上中点
 * 2)输入链表头节点，奇数长度返回中点，偶数长度返回下中点
 * 3)输入链表头节点，奇数长度返回中点前一个，偶数长度返回上中点前一个
 * 4)输入链表头节点，奇数长度返回中点前一个，偶数长度返回下中点前一个
 */
public class _快慢指针_01 {


    public static Node generalLinkNode(int[] nums) {
        if (nums == null || nums.length == 0) {
            return null;
        }
        Node header = new Node(nums[0], null);
        Node curNode = header;
        for (int i = 1; i < nums.length; i++) {
            Node node = new Node(nums[i], null);
            curNode.next = node;
            curNode = node;
        }

        return header;
    }

    public static void printLinkNode(Node header) {

        while (header != null) {
            System.out.print(header.num + ", ");
            header = header.next;
        }
        System.out.println();
    }

    //1) 输入链表头节点，奇数长度返回中点，偶数长度返回上中点
    public static Node findMiddlePreNode(Node header) {
        if (header == null || header.next == null) {
            return header;
        }
        Node slow = header;
        Node fast = header;
        while (fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }
        return null;
    }

    public static void main(String[] args) {
        int[] nums = {3,2,1,4,5,6};
        Node node = generalLinkNode(nums);
        printLinkNode(node);
    }
}
